package com.ehotting.edsta.circuitDesign.bean;

import com.alibaba.fastjson.JSON;
import com.ehotting.edsta.circuitDesign.enums.CircuitTypeEnum;
import com.ehotting.edsta.circuitDesign.enums.CodeDataTypeEnum;
import org.apache.commons.lang3.SerializationUtils;

import java.util.*;

/**
 * 全局参数类
 */
public class AllMapBean  extends HashMap<String,Map<String,Map<String,Object>>> implements Map<String,Map<String,Map<String,Object>>>,Cloneable{

    /**
     * 存放属性，如果不存在，创建对象，如果存在，追加属性值
     * @param circuitType，电路类型
     * @param codeDataType，数据类型
     * @param o
     */
    public void put(CircuitTypeEnum circuitType, CodeDataTypeEnum codeDataType,Map<String,Object> o){
        put( circuitType,  codeDataType, o,false);
    }

    public void append(CircuitTypeEnum circuitType, CodeDataTypeEnum codeDataType,Map<String,Object> o){
        put( circuitType,  codeDataType, o,true);
    }


    /**
     * 存放属性，如果不存在，创建对象，如果存在，追加属性值
     * @param circuitType，电路类型
     * @param codeDataType，数据类型
     * @param o
     */
    public void put(CircuitTypeEnum circuitType, CodeDataTypeEnum codeDataType,Map<String,Object> o,boolean isAppend){
        if(this.containsKey(circuitType.getCode())){
            Map<String,Map<String,Object>> circuitTypeMap = this.get(circuitType.getCode());
            if(circuitTypeMap.containsKey(codeDataType.getCode())){
                Map<String,Object> cdtMap = circuitTypeMap.get(codeDataType.getCode());

                putAll(cdtMap, o, isAppend);
                //cdtMap.putAll(o);
            }else {
                Map<String,Object> cdtMap = new HashMap<>();

                putAll(cdtMap, o, isAppend);
                //cdtMap.putAll(o);

                circuitTypeMap.put(codeDataType.getCode(),cdtMap);
            }
        }else{
            Map<String,Map<String,Object>> circuitTypeMap = new HashMap<>();
            this.put(circuitType.getCode(),circuitTypeMap);
            Map<String,Object> cdtMap = new HashMap<>();
            cdtMap.putAll(o);
            circuitTypeMap.put(codeDataType.getCode(),cdtMap);
        }
    }

    /**
     *
     * @param circuitType
     * @param codeDataType
     * @return
     */
    public Map<String,Object> get(CircuitTypeEnum circuitType, CodeDataTypeEnum codeDataType){
        if(this.containsKey(circuitType.getCode())) {
            Map<String, Map<String, Object>> circuitTypeMap = this.get(circuitType.getCode());
            if (circuitTypeMap.containsKey(codeDataType.getCode())) {
                Map<String,Object> cdtMap = circuitTypeMap.get(codeDataType.getCode());
                return cdtMap;
            }
        }
        return null;
    }

    /**
     * 取值
     * @param circuitType
     * @param codeDataType
     * @param key
     * @return
     */
    public Object get(CircuitTypeEnum circuitType, CodeDataTypeEnum codeDataType,String key){
        if(this.containsKey(circuitType.getCode())) {
            Map<String, Map<String, Object>> circuitTypeMap = this.get(circuitType.getCode());
            if (circuitTypeMap.containsKey(codeDataType.getCode())) {
                Map<String,Object> cdtMap = circuitTypeMap.get(codeDataType.getCode());
                return cdtMap.get(key);
            }
        }
        return null;
    }


    private void putAll(Map<String,Object> cdtMap,Map<String,Object> newMap,boolean isAdd){
        if(isAdd){
            appendAll(cdtMap, newMap);
        }else {
            cdtMap.putAll(newMap);
        }
    }

    private void appendAll(Map<String,Object> cdtMap,Map<String,Object> newMap){
        Set<Entry<String,Object>> kvSet =  newMap.entrySet();
        for(Entry<String,Object> kv : kvSet ){
            String key = kv.getKey();
            Object value = kv.getValue();
            if(cdtMap.containsKey(key)){
                Object o = cdtMap.get(key);


                if(o instanceof ArrayList){
                    ArrayList  oList =(ArrayList)o;
                    oList.add(String.valueOf(value));
                }else  {
                    if(String.valueOf(value).equals(String.valueOf(o))){
                        continue;
                    }
                    ArrayList  oList = new ArrayList();
                    oList.add(String.valueOf(o));
                    oList.add(String.valueOf(value));
                    cdtMap.put(key,oList);
                }
            }else{
                cdtMap.put(key,value);
            }
        }
    }

    public void clearProp(CircuitTypeEnum circuitType){
        if(this.containsKey(circuitType.getCode())){
            Map<String,Map<String,Object>> circuitTypeMap = this.get(circuitType.getCode());
            circuitTypeMap.remove(CodeDataTypeEnum.PROP.getCode());

        }
    }
//
//    public void clearProp(){
//        Set<Map.Entry<String,Map<String,Map<String,Object>>>> set =  this.entrySet();
//        for(Map.Entry<?,?> kv : set){
//            kv.getValue().r.remove(CodeDataTypeEnum.PROP.getCode());
//        }
//    }

    public AllMapBean clone(){
        return (AllMapBean) SerializationUtils.clone(this);
    }

}